package com.service.impl;

import com.entity.User;
import com.github.pagehelper.PageInfo;
import com.mapper.UserMapper;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public boolean login(String email, String password) {
        User user = userMapper.selectUserByEmail(email);
        if(user != null && user.getPassword().equals(password) && user.getStatus() == 1){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean register(String email, String password, String salt, String nickname, Integer status, String code, Date create_date) {
        if(nickname != null){
            userMapper.insertUser(new User(null,email,password,salt,nickname,status,code,create_date));
            return true;
        }else{
            return false;
        }
    }
    public boolean activeUser(String email, String code) {
        User user = userMapper.selectUserByEmail(email);
        if(user != null && user.getCode().equals(code)){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public void insertUser(User user) {
        userMapper.insertUser(user);
    }

    @Override
    public void deleteUserById(Integer id) {
        userMapper.deleteUserById(id);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

    @Override
    public User selectUserById(Integer id) {
        return userMapper.selectUserById(id);
    }

    @Override
    public User selectUserByEmail(String email) {
        return userMapper.selectUserByEmail(email);
    }

    @Override
    public PageInfo<User> searchUsers(Integer pageNum, Integer pageSize,String value) {
        return new PageInfo<>(userMapper.selectPageUser(pageNum,pageSize,null,value));
    }

    @Override
    public PageInfo<User> selectPageUsers(Integer pageNum, Integer pageSize,String query,String value) {
        return new PageInfo<>(userMapper.selectPageUser(pageNum,pageSize,query,value));
    }
}
